package jmetal.qualityIndicator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import jmetal.core.Problem;
import jmetal.core.Solution;
import jmetal.core.SolutionSet;
import jmetal.problems.DTLZ.DTLZ1;
import jmetal.problems.DTLZ.DTLZ2;
import jmetal.problems.DTLZ.DTLZ3;
import jmetal.problems.DTLZ.DTLZ4;

public class Main {
	public static void main(String[] args) throws IOException {
		Problem problem = new DTLZ1("Real",12,3);
		String pfDirect = "new_pf/" + problem.getName() + "_M" + problem.getNumberOfObjectives() + ".dat";
		System.out.println(pfDirect);
		QualityIndicator indicators = new QualityIndicator(problem, pfDirect);

		String outDirect = "results/NSGAIII/"+problem.getName()+"/IGD30_"+problem.getNumberOfObjectives()+".log";
		FileOutputStream output = new FileOutputStream(new File(outDirect));
		System.out.println(outDirect);
		for (int i = 0; i < 30; i++) {
			String path = "results/NSGAIII/"+problem.getName()+"/FUN_" + problem.getNumberOfObjectives() + "M_"
					+ i;
			System.out.println(path);
			
			//======修改此处
			SolutionSet population = setPopulation(path, 120);

			String out = "Run_" + i + ": " + indicators.getIGD(population) + "\n";
			output.write(out.getBytes());

			// String outDirect =
			// "results/NSGAIII/"+problem.getName()+"/"+problem.getNumberOfObjectives()+"M_"+i+".log";
			// Logger logger_ = Configuration.logger_;
			// FileHandler fileHandler_ = new FileHandler(outDirect);
			// logger_.addHandler(fileHandler_);
			// logger_.info("Quality indicators");
			// logger_.info("Hypervolume: " +
			// indicators.getHypervolume(population));
			// logger_.info("GD : " + indicators.getGD(population));
			// logger_.info("IGD : " + indicators.getIGD(population));
			// logger_.info("Spread : " + indicators.getSpread(population));
			// logger_.info("Epsilon : " +
			// indicators.getEpsilon(population));
		}
		output.close();
	}

	private static SolutionSet setPopulation(String path,int size) {


		SolutionSet population = new SolutionSet(size);

		try {
			// Open the file
			FileInputStream fis = new FileInputStream(path);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);

			int numberOfObjectives = 0;
			String aux = br.readLine();
			int i = 0;
			while (aux != null&&i<size) {
				StringTokenizer st = new StringTokenizer(aux);
				int j = 0;
				numberOfObjectives = st.countTokens();
				Solution solution = new Solution(numberOfObjectives);
				while (st.hasMoreTokens()) {
					double value = (new Double(st.nextToken())).doubleValue();
					solution.setObjective(j, value);
					
					// System.out.println("lambda["+i+","+j+"] = " + value) ;
					j++;
				}
				population.add(i, solution);
				aux = br.readLine();
				i++;
			}
			br.close();
		} catch (Exception e) {
			System.out
					.println("initUniformWeight: failed when reading for file: "
							+ "/" + path);
			e.printStackTrace();
		}

		return population;
	}
	
}
